/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.jdbc.SQLServerConnect;
import qlvbdq.objects.ObSanPham;

/**
 *
 * @author tuan
 */
public class MSanPham {
    SQLServerConnect connect;
    
    public MSanPham() {
    }
    
    public ArrayList<ObSanPham> GetSanPhamByRule(ObSanPham sp) throws SQLException {
        ArrayList<ObSanPham> listSanPham = new ArrayList<ObSanPham>();
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        ResultSet result = connect.getDataFromStore("GetSanPhamByRuleFromDatabase(?, ?, ?, ?, ?, ?, ?", objs);
        ObSanPham sanpham = null;
        while (result.next()) {
            sanpham = new ObSanPham(result.getString("MASP"), result.getString("TENSP"), result.getLong("DONGIABAN"),
                    result.getLong("DONGIAMUA"), result.getInt("SOLUONGTON"), result.getString("KIEUSANPHAM"), result.getString("LOAISANPHAM"));
            listSanPham.add(sanpham);
        }
        return listSanPham;
    }
    
    public int UpdateSanPham(ObSanPham sp) {
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        return connect.updateDataToDatabase("UpdateSanPhamToDatabase(?, ?, ?, ?, ?, ?, ?", objs);
    }
    
    public int DeleteSanPham(ObSanPham sp) {
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        return connect.updateDataToDatabase("DeleteSanPhamPNHToDatabase(?)", objs);
    }
    
    public int InsertSanPham(ObSanPham sp) {
        Object[] objs = { sp.getMaSP(), sp.getTenSP(), sp.getDonGiaBan(), 
            sp.getDonGiaMua(), sp.getSoLuongTon(), sp.getKieuSanPham(), sp.getLoaiSanPham() };
        return connect.updateDataToDatabase("InsertSanPhamToDatabase(?, ?, ?, ?, ?, ?, ?", objs);
    }
}
